﻿#region USING STATEMENTS
using System;
using System.Collections.Generic;
using Fusion.Management;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
#endregion

#region CODE BODY
namespace Fusion.Interface
{
    public class ControlList
    {
        // Data Fields
        List<Button> Buttons = new List<Button>();
        List<TextBox> TextBoxes = new List<TextBox>();
        TextBox FocusedTextBox = null;

        // Constructors
        public ControlList() { }

        // Public Static Methods
        public void Update(float Delta)
        {
            for (int i = 0; i < Buttons.Count; ++i)
                Buttons[i].Update(Delta);

            if (FInput.ButtonPressed(MouseButton.Left))
            {
                if (FocusedTextBox != null)
                    FocusedTextBox.Selected = false;

                FocusedTextBox = null;

                for (int i = 0; i < TextBoxes.Count; ++i)
                {
                    Rectangle BoxCollider = new Rectangle((int)TextBoxes[i].Position.X, (int)TextBoxes[i].Position.Y, (int)TextBoxes[i].Width, 24);

                    if (BoxCollider.Contains(FInput.MousePos()))
                    {
                        FocusedTextBox = TextBoxes[i];
                        FocusedTextBox.Selected = true;
                        break;
                    }
                }
            }

            if (FocusedTextBox != null)
                FocusedTextBox.Update(Delta);
        }
        public void Render(float Delta)
        {
            for (int i = 0; i < Buttons.Count; ++i)
                Buttons[i].Render(Delta);

            for (int i = 0; i < TextBoxes.Count; ++i)
                TextBoxes[i].Render(Delta);
        }
        public void AddTextBox(TextBox NewBox)
        {
            if (TextBoxes.Contains(NewBox))
                return;

            TextBoxes.Add(NewBox);
        }
        public TextBox AddTextBox(Vector2 Position, int Width, bool Password = false)
        {
            TextBox NewBox = new TextBox();
            NewBox.Position = Position;
            NewBox.Width = Width;
            NewBox.Password = Password;
            TextBoxes.Add(NewBox);
            return NewBox;
        }
        public void AddButton(Button NewButton)
        {
            if (Buttons.Contains(NewButton))
                return;

            Buttons.Add(NewButton);
        }
        public Button AddButton(string ButtonImageFromContent, string Text, Vector2 Position, Color Foreground)
        {
            Button NewButton = new Button(ButtonImageFromContent);
            NewButton.FaceText = Text;
            NewButton.Foreground = Foreground;
            NewButton.Position = Position;
            Buttons.Add(NewButton);
            return NewButton;
        }
        public void ClearButtons()
        {
            for (int i = 0; i < Buttons.Count; ++i)
            {
                Buttons[i].Dispose();
                Buttons[i] = null;
            }

            Buttons.Clear();

            GC.Collect();
        }
        public void ClearTextBoxes()
        {
            for (int i = 0; i < TextBoxes.Count; ++i)
            {
                TextBoxes[i].Dispose();
                TextBoxes[i] = null;
            }

            TextBoxes.Clear();

            GC.Collect();
        }
        public void ClearControls()
        {
            ClearButtons();
            ClearTextBoxes();
        }
        public void RemoveButton(Button ToRemove)
        {
            if (Buttons.Contains(ToRemove))
                Buttons.Remove(ToRemove);
        }
        public void RemoveTextBox(TextBox ToRemove)
        {
            if (TextBoxes.Contains(ToRemove))
                TextBoxes.Remove(ToRemove);
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////